﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using automobili_data;
using automobili_data.interfaces;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Net.Mail;




namespace automobili_gui.UserManagement
{
    public partial class Detalji_automobil : System.Web.UI.Page
    {
        User k = null;
        Privilegije p = null;
        int j=0;
        Document doc = new Document();
        IList<User> korisnik = null;
       
        protected void Page_Load (object sender, EventArgs e)
        {
            p = SessionUtils.getCurrentPrivileges();
            if (p == null) { p = AutomobiliDataManager.instance.dohvatiPodrazPrivilegije(); }
            if (!Page.IsPostBack)
            {
                if (p.PrikazDetalja_s == 1)
                {
                    int i = Int32.Parse(Request.QueryString["id"]);

                    IList<Automobil> automobili = AutomobiliDataManager.instance.dohvatiDetalje(i);
                    k = SessionUtils.getCurrentUser();



                    GridView3.DataSource = automobili;

                    GridView3.DataBind();
                    if (p.Registracije_s == 1 && k != null)
                    {
                        korisnik = AutomobiliDataManager.instance.dohvatiKorisnika(k.UserId);
                        //  k = SessionUtils.getCurrentUser();
                        p = SessionUtils.getCurrentPrivileges();
                        PdfWriter.GetInstance(doc,
                       new System.IO.FileStream("D:\\prvi" + k.UserName + " " + automobili[0].Id + ".pdf", System.IO.FileMode.Create));
                        doc.Open();
                    }
                    else
                    {
                        korisnik = null;
                    }
                    if (k != null && p.Pretraga_s == 1)
                    {

                        IList<User> u = AutomobiliDataManager.instance.dohvatiKontakt(i);
                        Label1.Text = "Kontakt: " + " " + u[0].Name + " " + u[0].Surname + " " + u[0].UserName;

                        dodajOglasavacaPdf(doc, u);


                    }

                    if (k != null && p.Rezervisanje_k == 1)
                    {

                        dodajAutomobilPdf(doc, automobili);
                        dodajKorisnikaPdf(doc, korisnik);



                    }
                    doc.Close();
                }
            }
           
        }

        public void dodajAutomobilPdf (Document d, IList<Automobil> automobili)
        {
            PdfPTable header0 = new PdfPTable(1);

            header0.DefaultCell.BackgroundColor = BaseColor.WHITE;
            Font font = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLACK);
            Phrase p0 = new Phrase("ID rezervisanog automobila: " + automobili[0].Id);
            header0.AddCell(p0);
            Phrase p1 = new Phrase("Marka: " + automobili[0].Marka, font);
            header0.AddCell(p1);
            Phrase p2 = new Phrase("Model: " + automobili[0].Model, font);
            header0.AddCell(p2);
            Phrase p3 = new Phrase("Pogonsko gorivo: " + automobili[0].Gorivo);
            header0.AddCell(p3);
            Phrase p4 = new Phrase("Cena: " + automobili[0].Cena);
            header0.AddCell(p4);
            Phrase p5 = new Phrase("Oblik karoserije: " + automobili[0].Karoserija);
            header0.AddCell(p5);
            Phrase p6 = new Phrase("Godina proizvodnje: " + automobili[0].GodinaProizvodnje);
            header0.AddCell(p6);
            header0.SpacingBefore = 5f;
            header0.SpacingAfter = 5f;
            d.Add(header0);
        }

        public void dodajOglasavacaPdf (Document d, IList<User> u)
        {

            PdfPTable header = new PdfPTable(1);
            header.DefaultCell.BackgroundColor = BaseColor.WHITE;
            Font font = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLACK);
            Phrase p = new Phrase("Ime oglasavaca: " + u[0].Name, font);
            header.AddCell(p);
            Phrase p0 = new Phrase("Prezime oglasavaca: " + u[0].Surname, font);
            header.AddCell(p0);
            Phrase p1 = new Phrase("Korisnicko ime oglasavaca: " + u[0].UserName, font);
            header.AddCell(p1);
            header.SpacingBefore = 5f;
            header.SpacingAfter = 5f;
            d.Add(header);


        }

        public void dodajKorisnikaPdf (Document d, IList<User> u)
        {

            PdfPTable header = new PdfPTable(1);
            header.DefaultCell.BackgroundColor = BaseColor.WHITE;
            Font font = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLACK);
            Phrase p = new Phrase("Ime korisnika: " + u[0].Name, font);
            header.AddCell(p);
            Phrase p0 = new Phrase("Prezime korisnika: " + u[0].Surname, font);
            header.AddCell(p0);
            Phrase p1 = new Phrase("Korisnicko ime korisnika: " + u[0].UserName, font);
            header.AddCell(p1);
            header.SpacingBefore = 5f;
            header.SpacingAfter = 5f;
            d.Add(header);


        }

        protected void rezervisi (object sender, GridViewCommandEventArgs e)
        {
            if ((k != null) && (p.Rezervisanje_k == 1))
            {
                if (e.CommandName == "rezervisi")
                {
                    Global.sem.WaitOne();
                    k = SessionUtils.getCurrentUser();
                    GridView3.SelectRow(1);
                    int index = Convert.ToInt32(e.CommandArgument);
                    int idAu = Int32.Parse(GridView3.Rows[index].Cells[0].Text);

                    AutomobiliDataManager.instance.kreirajRezervaciju(k.UserId, idAu);
                    IList<Rezervacija> rezerv = AutomobiliDataManager.instance.podaciZaRezervaciju();
                    int idRez = rezerv[0].IdRez;
                    int idOgl = rezerv[0].IdOgl;

                    this.Send(idRez, idOgl, idAu);
                    Global.sem.Release();

                }
            }
            else 
            {
                Label1.Text = "Morate da se ulogujete da bi ste rezervisali automobil!";
            }
        }

        private void SendEMail (MailMessage mailMessage)
        {
            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential("automobil.portal@gmail.com", "iep_2013");
            client.Host = "smtp.gmail.com";

            try
            {
                client.Send(mailMessage);
            }
            catch (SmtpException)
            {
                throw;
            }
        }

        public void Send (int idR, int idO, int idAu)
        {
            MailMessage Message = new MailMessage();
            MailAddress Email = new MailAddress("jeca_dk@yahoo.com");
            MailAddress Email1 = new MailAddress("automobil.portal@gmail.com");
            Message.To.Add(Email);
            Message.To.Add(Email1);
            Message.From = new MailAddress("automobil.portal@gmail.com");
            Message.Subject = "Id rezervacije " + idR + ". Id oglasa " + idO;
            Message.Body = "Id vase rezervacije je: " + idR + ". Rezervisali ste automobil sa brojem oglasa: " + idO;
            Attachment Attachment = new Attachment(@"D:\\prvi" + k.UserName +" "+idAu+ ".pdf");
            Attachment.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
            Message.Attachments.Add(Attachment);
            SendEMail(Message);
            
        }

        //static PdfPTable napraviTabelu ()
        //{
        //    PdfPTable pdft = new PdfPTable(6);
        //    PdfPCell cell = new PdfPCell(new Phrase("Proba"));
        //    pdft.AddCell(cell);
        //    return pdft;
        //}

        //
        //private void addHeaderTable (Document d)
        //{
        //    PdfPTable header1 = new PdfPTable(3);
        //    PdfPTable header0 = new PdfPTable(1);
        //    header1.WidthPercentage = (100);



        //    header1.DefaultCell.BackgroundColor = BaseColor.WHITE;
        //    Font font = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLACK);

        //    IList<User> u = AutomobiliDataManager.instance.dohvatiKontakt(ii);
        //    header0.DefaultCell.BackgroundColor = BaseColor.WHITE;
        //    //Phrase p0 = new Phrase("Datum rezervisanja: "+DateTime.Now, font);
        //    //header0.AddCell(p0);
        //    //Phrase p1 = new Phrase("Ime oglasavaca: "+ users[0].Name, font);
        //    //header1.AddCell(p1);
        //    //Phrase p2 = new Phrase("Prezime oglasavaca: "+ users[0].Surname, font);
        //    //header1.AddCell(p1);
        //    //Phrase p3 = new Phrase("Korisnicko ime oglasavaca"+ users[0].UserName, font);
        //    //header1.AddCell(p1);


        //    // header.DefaultCell.HorizontalAlignment = (Element.ALIGN_CENTER);
        //    //   p1 = new Phrase("2013/05/15", font);
        //    //   header1.AddCell(p1);

        //    //header.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
        //    // p1 = new Phrase("page 1", font);
        //    //header1.AddCell(p1);

        //    d.Add(header0);
        //    d.Add(header1);
        //    d.Add(new Paragraph());
        //}

        //public void generatePDFTest (Document d)
        //{
        //    // step 1
        //    Document document = new Document();
        //    // step 2
        //    PdfWriter.GetInstance(document,
        //        new System.IO.FileStream("D:\\prvi" + k.UserName + ".pdf", System.IO.FileMode.Create));

        //    // step 3
        //    document.Open();


        //    addHeaderTable(d);
        //    // step 4
        //    document.Add(napraviTabelu());


        //    // step 5
        //    document.Close();
        //}
    }
}